﻿Public Class Kullanici

	Shared Function SifreDegistir(ByVal Kod As Integer, ByVal Kimlik As String, ByVal Password As String) As String
		Dim Rs As Recordset = OpenSQL("select * from personel where kod = @kod", Kod)
		If Rs("kimlik") <> Kimlik Then Return "kh"
		ExecSQL("update personel set password = @pwd where kod = @kod", EncrDecr(True, Password), Kod)
		Return ""
	End Function

	Shared Sub Oku(Uygulama As String, ByVal Kod As Integer, ByVal Password As String)
		If Kod = 0 Then Return
		Kullanici.Uygulama = Uygulama
		Kullanici.Kod = Kod
		Dim Rs = OpenSQL("select * from personel where kod = @kod", Kod)
		Kullanici.Ad = Rs("adi")
		Kullanici.Soyad = Rs("soyadi")
		Select Case My.Computer.Name.ToLower
			Case "ssd-ahmet"
			Case Else
				If IsDBNull(Rs("password")) Then
					Kullanici.Durum = KullaniciDurumu.MustChangePassword
				ElseIf EncrDecr(False, Rs("password")) <> Password Then
					Kullanici.Durum = KullaniciDurumu.InvalidPassword
				Else
					Kullanici.Durum = KullaniciDurumu.OK
				End If
		End Select
		Kullanici.SetYetki(IsNull(Rs("yetki"), ""))
		Kullanici.Birim = Rs("birim")
	End Sub

	Shared Sub SifreSifirla(ByVal Kod As Integer)
		ExecSQL("update personel set password = null where kod = @kod", Kod)
	End Sub

	Shared Function SonAcilanlar(ByVal Kullanici As Integer) As List(Of SourceItem)
		Dim List As New List(Of SourceItem)
		Dim Rs = OpenSQL("select a.kod, a.adi, a.soyadi from personel a inner join log b on a.kod = b.tkod where b.kullanici = @user and b.tablo = 'open' order by b.kod desc", Kullanici)
		Dim i As Integer
		Do Until Rs.EOF
			If List.Find(Function(x) x.Kod = Rs("kod")) Is Nothing Then
				List.Add(New SourceItem(Rs("kod"), ToTitle(Rs("adi") & " " & Rs("soyadi"))))
				i += 1
				If i = 8 Then Exit Do
			End If
			Rs.MoveNext()
		Loop
		Return List
	End Function

	Shared Function Birimler() As List(Of SourceItem)
		Dim List As New List(Of SourceItem)
		Dim Rs = OpenSQL("select kod,adi from birim where ust is not null order by adi")
		Do Until Rs.EOF
			List.Add(New SourceItem(Rs("kod"), Rs("adi")))
			Rs.MoveNext()
		Loop
		Return List
	End Function

	Shared Function KullaniciAdi(ByVal Kod As Integer) As String
		Dim Rs = OpenSQL("select kod, adi, soyadi from personel where kod = @kod", Kod)
		Return ToTitle(Rs("adi") & " " & Rs("soyadi"))
	End Function

	Shared Function Bilgi(ByVal Kod As Integer) As Recordset
		Return OpenSQL("select * from personel where kod = @kod", Kod)
	End Function

	Shared Function Liste(ByVal Birim As Integer) As List(Of SourceItem)
		Dim List As New List(Of SourceItem)
		Dim Rs = OpenSQL("select kod, adi, soyadi from personel where isnull(durum,'') <> 'x' and birim = " & If(Birim = 0, "birim", Birim) & " order by adi,soyadi")
		Do Until Rs.EOF
			List.Add(New SourceItem(Rs("kod"), ToTitle(Rs("adi") & " " & Rs("soyadi"))))
			Rs.MoveNext()
		Loop
		Return List
	End Function

	Public Shared Kod As Integer
	Public Shared Ad, Soyad As String
	Public Shared Birim As Integer = -1
	Public Shared Durum As KullaniciDurumu
	Public Shared Uygulama As String
	Public Shared Yetkiler As New List(Of Yetki)

	Shared Sub SetYetki(Text As String)
		If Text = "" Then Return
		For Each Yetki As String In Text.Split(",")
			Dim Items = Yetki.Split(".")
			If Items.First = Uygulama Then Yetkiler.Add(New Yetki(Items(1)))
		Next
	End Sub

	Public Enum KullaniciDurumu
		OK
		InvalidPassword
		MustChangePassword
	End Enum

	Shared ReadOnly Property [Operator]() As Boolean
		Get
			Return (From y In Yetkiler Where y.Tur = "opr").Any
		End Get
	End Property

	Shared ReadOnly Property [Yonetici]() As Boolean
		Get
			Return (From y In Yetkiler Where y.Tur = "mgr").Any
		End Get
	End Property

	Shared ReadOnly Property OprBirim As Integer
		Get
			Return (From y In Yetkiler Where y.Tur = "opr").FirstOrDefault.Birim
		End Get
	End Property

	Shared ReadOnly Property MgrBirim As Integer
		Get
			Return (From y In Yetkiler Where y.Tur = "mgr").FirstOrDefault.Birim
		End Get
	End Property

	Shared ReadOnly Property TBirim As Integer
		Get
			If Yonetici Then Return MgrBirim
			If [Operator] Then Return OprBirim
			Return -1
		End Get
	End Property

End Class

Public Class Yetki

	Public Tur As String
	Public Birim As Integer

	Sub New(Def As String)
		Dim Pos = InStr(Def, "(")
		Tur = Mid(Def, 1, Pos - 1)
		Birim = Val(Mid(Def, Pos + 1))
	End Sub

End Class